package be.one.paq.ui.component.user;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import be.one.paq.model.User;
import be.one.paq.service.IUserService;

import com.vaadin.data.util.BeanContainer;
import com.vaadin.ui.Table;

@Configurable(preConstruction = true)
public class UserTable extends Table {

	private static final long serialVersionUID = 1L;

	@Autowired
	transient IUserService userService;

	public UserTable() {
		setSizeFull();
		setImmediate(true);
		setSelectable(true);
		setContainerDataSource(getTableContainer());
        setVisibleColumns(getTableColumns());
	}

	public BeanContainer<Integer, User> getTableContainer() {
		BeanContainer<Integer, User> container = new BeanContainer<Integer, User>(User.class);
		container.setBeanIdProperty("id");
		for (User entity : userService.findAll()) {
			container.addBean(entity);
		}
		return container;
	}
	
	public Object[] getTableColumns() {
        ArrayList<Object> columnIds = new ArrayList<Object>();
        
        for (Object property : getContainerPropertyIds()) {
            if (property != null && property.equals("id")) {
                columnIds.add(0, property);
            }
            if (property != null && property.equals("login")) {
                columnIds.add(1, property);
            }
            if (property != null && property.equals("password")) {
                columnIds.add(2, property);
            }
            if (property != null && property.equals("firstname")) {
                columnIds.add(3, property);
            }
            if (property != null && property.equals("lastname")) {
                columnIds.add(4, property);
            }
        }
        return columnIds.toArray();
    }

}
